package sealte.mytable.run;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JTable;

import sealte.mytable.frame.MainFrame;
import sealte.mytable.frame.Save2DBDialog;
import sealte.mytable.util.DBConnection;
import sealte.mytable.util.UtilTools;

public class MyTable {
	
	private static MainFrame mf = null;
	private static Save2DBDialog sf = null;
	
	public static void main(String[] args){
		MainFrame.setDefaultLookAndFeelDecorated(true);
		mf = new MainFrame();
		mf.instance();
		Save2DBDialog.setDefaultLookAndFeelDecorated(true);
		sf = new Save2DBDialog(mf);
		sf.doInit();
		mf.getJb_showTable().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int res = JOptionPane.showConfirmDialog(null, "Are you sure?");
				if(res==0){
					String rows_str = mf.getJtf_rows().getText().trim();
					String columns_str = mf.getJtf_columns().getText().trim();
					if(!rows_str.equals("")){
						if(!columns_str.equals("")){
							if(UtilTools.isNumber(rows_str)){
								if(UtilTools.isNumber(columns_str)){
									int rows = Integer.valueOf(rows_str);
									int columns = Integer.valueOf(columns_str);
									if(rows>0 && rows<=100 && columns>0 && columns<=100){
										mf.getJp_center().removeAll();
										mf.setJt_content(rows, columns);
										mf.getJt_content().setRowHeight(25);
										mf.getJt_content().setAutoCreateRowSorter(true);
										mf.setJsp_content(mf.getJt_content());
										mf.getJp_center().add(mf.getJsp_content(),BorderLayout.CENTER);
										mf.getJp_center().repaint();
										mf.getJp_center().validate();
										mf.repaint();
										mf.validate();
										mf.pack();
									}else{
										JOptionPane.showMessageDialog(null, "Rows and Columns should between 1 and 100");
									}
								}else{
									JOptionPane.showMessageDialog(null, "Columns should be an NUMBER!");
								}
							}else{
								JOptionPane.showMessageDialog(null, "Rows should be an NUMBER!");
							}
						}else{
							JOptionPane.showMessageDialog(null, "Columns cannot be NULL!");
						}
					}else{
						JOptionPane.showMessageDialog(null, "Rows cannot be NULL!");
					}
				}	
			}
		});
		mf.getJb_exportExcel().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int res = JOptionPane.showConfirmDialog(null, "Are you sure?");
				if(res==0){
					JTable jt = mf.getJt_content();
					if(null!=jt){
						int rows = jt.getRowCount();
						int columns = jt.getColumnCount();
						String[][] content = new String[rows][columns];
						for(int i=0;i<content.length;i++){
							for(int j=0;j<content[i].length;j++){
								content[i][j] = (String)jt.getValueAt(i, j);
							}
						}
						// Export Excel
						FileOutputStream fos = null;
						try {
							fos = new FileOutputStream(System.currentTimeMillis()+".xls");
							String res_str = UtilTools.exportExcel(content, fos);
							JOptionPane.showMessageDialog(null, res_str);
						}catch(FileNotFoundException e1) {
							e1.printStackTrace();
						}finally{
							if(null!=fos){
								try {
									fos.close();
								} catch (IOException e1) {
									e1.printStackTrace();
								}
							}
						}
					}else{
						JOptionPane.showMessageDialog(null, "Please initial the Table first!");
					}
				}
			}
		});
		mf.getJb_exportTXT().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int res = JOptionPane.showConfirmDialog(null, "Are you sure?");
				if(res==0){
					JTable jt = mf.getJt_content();
					if(null!=jt){
						int rows = jt.getRowCount();
						int columns = jt.getColumnCount();
						String[][] content = new String[rows][columns];
						for(int i=0;i<content.length;i++){
							for(int j=0;j<content[i].length;j++){
								content[i][j] = (String)jt.getValueAt(i, j);
							}
						}
						// Export TXT
						FileOutputStream fos = null;
						try {
							fos = new FileOutputStream(System.currentTimeMillis()+".txt");
							String res_str = UtilTools.exportTXT(content, fos);
							JOptionPane.showMessageDialog(null, res_str);
						}catch(FileNotFoundException e1) {
							e1.printStackTrace();
						}finally{
							if(null!=fos){
								try {
									fos.close();
								} catch (IOException e1) {
									e1.printStackTrace();
								}
							}
						}
					}else{
						JOptionPane.showMessageDialog(null, "Please initial the Table first!");
					}
				}
			}
		});
		mf.getJb_exportXML().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int res = JOptionPane.showConfirmDialog(null, "Are you sure?");
				if(res==0){
					JTable jt = mf.getJt_content();
					if(null!=jt){
						int rows = jt.getRowCount();
						int columns = jt.getColumnCount();
						String[][] content = new String[rows][columns];
						for(int i=0;i<content.length;i++){
							for(int j=0;j<content[i].length;j++){
								content[i][j] = (String)jt.getValueAt(i, j);
							}
						}
						// Export XML
						FileOutputStream fos = null;
						try {
							fos = new FileOutputStream(System.currentTimeMillis()+".xml");
							String res_str = UtilTools.exportXML(content, fos);
							JOptionPane.showMessageDialog(null, res_str);
						}catch(FileNotFoundException e1) {
							e1.printStackTrace();
						}finally{
							if(null!=fos){
								try {
									fos.close();
								} catch (IOException e1) {
									e1.printStackTrace();
								}
							}
						}
					}else{
						JOptionPane.showMessageDialog(null, "Please initial the Table first!");
					}
				}
			}
		});
		mf.getJb_save2DB().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JTable jt = mf.getJt_content();
				if(null!=jt){
					sf.setVisible(true);
				}else{
					JOptionPane.showMessageDialog(null, "Please initial the Table first!");
				}
			}
		});
		sf.getJb_save().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JTable jt = mf.getJt_content();
				int rows = jt.getRowCount();
				int columns = jt.getColumnCount();
				String[][] content = new String[rows][columns];
				for(int i=0;i<content.length;i++){
					for(int j=0;j<content[i].length;j++){
						content[i][j] = (String)jt.getValueAt(i, j);
					}
				}
				String db_style = (String)sf.getJcb_db_style().getSelectedItem();
				String db_ip_port = sf.getJtf_db_ip_port().getText();
				String db_name = sf.getJtf_db_name().getText();
				String db_user = sf.getJtf_db_user().getText();
				String db_password = new String(sf.getJpf_db_password().getPassword());
				String sql = sf.getJta_content().getText();
				
				if("".equals(db_ip_port)){
					db_ip_port = "127.0.0.1:3306";
				}
				if("".equals(db_name)){
					db_name = "fuck_ports";
				}
				if("".equals(db_user)){
					db_user = "root";
				}
				if("".equals(db_password)){
					db_password = "admin";
				}
				if("".equals(sql)){
					JOptionPane.showMessageDialog(null, "Create-Table-Script must have something!");
					return ;
				}
				DBConnection dbc = new DBConnection(db_style,db_ip_port,db_name,db_user,db_password);
				String result = UtilTools.save2DB(content,dbc,sql);
				sf.getJta_content().append("\n--#----------------#----------------#--\n" + result);
			}
		});
		sf.getJb_cancel().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sf.setVisible(false);
			}
		});
	}
}
